#include "qmailmodel.h"

// QMailModel::QMailModel(QObject *parent)
//     : QAbstractListModel(parent)
// {}

QVariant QMailModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role!= Qt::DisplayRole) {
        return QVariant();
    }
    if(orientation == Qt::Horizontal) {
        return QString("Column % 1").arg(section);
    }
    else {
        return QString("Row % 1").arg(section);
    }
}

int QMailModel::rowCount(const QModelIndex &parent) const
{
    return stringList.count();
}

QVariant QMailModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if(index.row() >= stringList.size()) {
        return QVariant();
    }

    if(role == Qt::DisplayRole||role == Qt::EditRole) {
        return stringList.at(index.row());
    }
    else {
        return QVariant();
    }
}

Qt::ItemFlags QMailModel::flags(const QModelIndex &index) const {
    if(!index.isValid()) {
        return Qt::ItemIsEnabled;
    }
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool QMailModel::setData(const QModelIndex &index,const QVariant &value,int role) {
    if(index.isValid() && role == Qt::EditRole) {
        stringList.replace(index.row(),value.toString());
        emit dataChanged(index,index);
        return true;
    }
    return false;
}

/**
 * 增加元素.
 * 需要告知列表数据增加了.
 * @brief QMailModel::insertRows
 * @param position
 * @param rows
 * @param index
 * @return
 */
bool QMailModel::insertRows(int position,int rows ,const QModelIndex &index) {
    beginInsertRows(QModelIndex(),position,position+rows-1);
    for (int row = 0;row < rows; ++row) {
        stringList.insert(position,"aaa");
    }
    endInsertRows();
    return true;
}

bool QMailModel::removeRows(int position,int rows ,const QModelIndex &index) {
    beginRemoveRows(QModelIndex(),position,position+rows-1);
    for (int row = 0;row < rows; ++row) {
        stringList.removeAt(position);
    }
    endRemoveRows();
    return true;
}
